#include "stdafx.h"

namespace classifier_test
{

discr_value classifier::classify(const record& Record) const
{
	array<double> P;
	estimate(Record, P);
	return max_element(P.begin(), P.end()) - P.begin();
}

void normalize_probabilities(array<double>& Probs)
{
	double Sum = 0;
	for (int I = 0; I < Probs.size(); I++) Sum += Probs[I];
	if (Sum <= 0 || Probs.size() == 1) Probs.assign(Probs.size(), 1.0 / Probs.size());
	else for (int I = 0; I < Probs.size(); I++) Probs[I] /= Sum;
}

void classification_results(classifier* Classifier, subarray<const record* const> Data, vector<discr_value>& Results)
{
	Results.resize(Data.size());
	for (int I = 0; I < Data.size(); I++) Results[I] = Classifier->classify(*Data[I]);
}

int classification_errors(subarray<const record* const> Data, subarray<discr_value> Results)
{
	assert(Data.size() == Results.size());
	int Errors = 0;
	for (int I = 0; I < Data.size(); I++) if (Results[I] != Data[I]->back().discr) Errors++;
	return Errors;
}

double classification_error(subarray<const record* const> Data, subarray<discr_value> Results)
{
	return (double)classification_errors(Data, Results) / Data.size();
}

int classification_errors(classifier* Classifier, subarray<const record* const> Data)
{
	int Errors = 0;
	for (int I = 0; I < Data.size(); I++) if (Classifier->classify(*Data[I]) != Data[I]->back().discr) Errors++;
	return Errors;
}

double classification_error(classifier* Classifier, subarray<const record* const> Data)
{
	return (double)classification_errors(Classifier, Data) / Data.size();
}

double classification_similarity(subarray<const discr_value> A, subarray<const discr_value> B)
{
	assert(A.size() == B.size());
	int Sames = 0;
	for (int I = 0; I < A.size(); I++) Sames += A[I] == B[I];
	return (double)Sames / A.size();
}

}
